import * as API from '../apis/shareSetting'
import {observable, action} from 'mobx';
import {message} from 'antd';
import {WeaTableNew, WeaForm} from 'comsMobx'
const {TableStore} = WeaTableNew;
import {WeaLocaleProvider} from "ecCom";
const getLabel = WeaLocaleProvider.getLabel;

class ShareSetting {
  @observable defaultShareTable = new TableStore();
  @observable myShareTable = new TableStore();

  @observable status = {
    selectedKey: '1',
    addShareCondition: [],
    addShareVisible: false,
    editShareCondition:[],
    editShareVisible: false,
    shareId:''
  }

  @action

  doInit = () => {
    this.getBlogShareSetListDefaultCondition();
    this.getBlogShareSetListCondition();
    this.getBlogShareSetAddCondition();
  }

  setState = (params = {}) => {
    let status = {...this.status};
    Object.keys(params).forEach(key => {
      status[key] = params[key];
    });
    this.status = status;
  }

  //默认分享
  getBlogShareSetListDefaultCondition = () => {
    API.getBlogShareSetListDefaultCondition().then(result => {
      this.defaultShareTable.getDatas(result.sessionkey)
    })
  }

  //我的分享
  getBlogShareSetListCondition = () => {
    API.getBlogShareSetListCondition().then(result => {
      this.myShareTable.getDatas(result.sessionkey)
    })
  }

  //我的分享添加规则
  getBlogShareSetAddCondition = () => {
    API.getBlogShareSetAddCondition().then(result => {
      this.status.addShareCondition = result.conditioninfo;
    })
  }

  //我的分享编辑规则
  getBlogShareSetEditCondition = (params) => {
    API.getBlogShareSetEditCondition(params).then(result => {
      this.status.editShareCondition = result.conditioninfo;
    })
  }

  operateBlogShareSet = (params) => {
    API.operateBlogShareSet(params).then(result => {
      this.status.addShareVisible = false;
      this.status.editShareVisible = false;
      if(result.status == 1){
        message.success(getLabel(83885, '操作成功'));
        this.getBlogShareSetListCondition();
      }else {
        message.error(getLabel(4715, '操作失败'));
      }
    })
  }

}

const blogShareSetting = new ShareSetting();
export default blogShareSetting;